package com.iman.sumer.service.impl.student;

import java.util.HashMap;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.iman.common.Constants;
import com.iman.common.hibernate.HibernateSupport;
import com.iman.common.hibernate.TemplateQLService;
import com.iman.common.support.Page;
import com.iman.sumer.service.student.CourseService;
import com.iman.sumer.vo.CommentVo;
import com.iman.sumer.vo.CourseClassmateVo;
import com.iman.sumer.vo.CourseCtgVo;
import com.iman.sumer.vo.CourseNoteVo;
import com.iman.sumer.vo.CourseTeacherVo;
import com.iman.sumer.vo.CourseVo;
import com.iman.sumer.vo.ExamVo;
import com.iman.sumer.vo.HomeworkVo;
import com.iman.sumer.vo.ResourceVo;
import com.iman.sumer.vo.StudyCourseVo;
import com.iman.sumer.vo.student.StudyDmsgVo;
import com.iman.sumer.vo.student.StudyRemarkVo;

/**
 * @description 我的课程Service层实现
 * @author huangt
 * @date 2014年12月24日
 */
@Service("courseService")
public class CourseServiceImpl implements CourseService {
	@Inject
    TemplateQLService templateQLService;

    @Inject
    HibernateSupport  hibernateSupport;
	
    /**
 	 * @description 获得user所有学习动态消息表
 	 * @param map
 	 * @return Page<StudyRemarkVo>
 	 * @throws Exception 
 	 * @author syd  2014年12月29日
 	 */
    @Override
	public Page<StudyRemarkVo> getStudyRemarkList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		long userId = (Long) map.get("userId");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", userId);
		Page<StudyRemarkVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-study-remark-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,StudyRemarkVo.class);
		return  list;
	}
    /**
	 * @description 获得user所有学习动态消息表
	 * @param map
	 * @return Page<StudyDmsgVo>
	 * @throws Exception 
	 * @author syd  2014年12月28日
	 */
    @Override
	public Page<StudyDmsgVo> getAllStudyDmsgList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		long userId = (Long) map.get("userId");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", userId);
		Page<StudyDmsgVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-dmsg-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,StudyDmsgVo.class);
		return  list;
	}
	/**
	 * @description 获得所有课程列表
	 * @param map
	 * @return Page<CourseVo>
	 * @throws Exception 
	 * @author huangt  2014年12月24日
	 */
	public Page<CourseVo> getAllCourseList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		long userId = (Long) map.get("userId");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", userId);
		hashMapParams.put("courseId", map.get("courseId"));
		hashMapParams.put("courseSubject", map.get("courseSubject"));
		hashMapParams.put("courseOnlinetp", map.get("courseOnlinetp"));
		Page<CourseVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CourseVo.class);
		return  list;
	}
	
	/**
	 * @description 根据ID获得教师信息
	 * @param id
	 * 			教师ID
	 * @return CourseTeacherVo
	 * @throws Exception 
	 * @author huangt  2015年1月4日
	 */
	public CourseTeacherVo getCourseTeacherById(long id) throws Exception {
		Integer currentPage = 0;
		Integer pageSize  = 5;
		CourseTeacherVo teacher = new CourseTeacherVo();
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", id);
		Page<CourseTeacherVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-teacher", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CourseTeacherVo.class);
		if (list.getTotalCount() > 0) {
			teacher = list.getData().get(0);
		}
		return  teacher;
	}
	
	/**
	 * @description 根据类型获得课程列表
	 * @param map
	 * @return Page<CourseVo>
	 * @throws Exception 
	 * @author huangt  2014年12月25日
	 */
	public Page<CourseVo> getCourseListByType(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		long userId = (Long) map.get("userId");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", userId);
		hashMapParams.put("courseType", map.get("courseType"));
		Page<CourseVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-type-course-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CourseVo.class);
		return  list;
	}
	
	/**
	 * @description 获得专题课程的课程列表
	 * @param map
	 * @return Page<CourseVo>
	 * @throws Exception 
	 * @author huangt  2014年12月26日
	 */
	public Page<CourseVo> getTopicCourseList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		long userId = (Long) map.get("userId");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("userId", userId);
		hashMapParams.put("courseId", map.get("courseId"));
		hashMapParams.put("subjectId", map.get("subjectId"));
//		hashMapParams.put("courseSubject", map.get("courseSubject"));
		Page<CourseVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-topic-course-list", hashMapParams, Page.getStartOfPage(currentPage,pageSize), pageSize,CourseVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课程课件列表
	 * @return Page<CourseCtgVo>
	 * @throws Exception 
	 * @author huangt  2014年12月26日
	 */
	public Page<CourseCtgVo> getCourseCtgList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("courseId", map.get("courseId"));
		Page<CourseCtgVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-category", hashMapParams, 
				Page.getStartOfPage(currentPage, pageSize), pageSize, CourseCtgVo.class);
		if (list.getData() != null) {
			//设置课时对应的作业和考试  huangt  2014-12-30
			for (CourseCtgVo courseCtgVo : list.getData()) {
				long ctgId = courseCtgVo.getId();
				hashMapParams.put("categoryId", ctgId);
				Page<HomeworkVo> homework = templateQLService.findByPagedSQLBeanQuery("query-course-ctg-homework", 
						hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, HomeworkVo.class);;
				if (homework.getData() != null  && homework.getData().size()>0) {
					courseCtgVo.setHomeworkVo(homework.getData().get(0));
				}
				Page<ExamVo> exam = templateQLService.findByPagedSQLBeanQuery("query-course-ctg-exam", 
						hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, ExamVo.class);
				if (exam.getData() != null && exam.getData().size()>0 ) {
					courseCtgVo.setExamVo(exam.getData().get(0));
				}
			}
		}
		return  list;
	}
	
	/**
	 * @description 获得课时作业列表
	 * @param map
	 * @return Page<HomeworkVo>
	 * @throws Exception 
	 * @author huangt  2014年12月30日
	 */
	public Page<HomeworkVo> getCtgHomework(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("categoryId", map.get("categoryId"));
		hashMapParams.put("homeworkId", map.get("homeworkId"));
		Page<HomeworkVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-ctg-homework", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, HomeworkVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课时考试列表
	 * @param map
	 * @return Page<ExamVo>
	 * @throws Exception 
	 * @author huangt  2014年12月30日
	 */
	public Page<ExamVo> getCtgExam(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("categoryId", map.get("categoryId"));
		Page<ExamVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-ctg-exam", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, ExamVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课时资源列表
	 * @param map
	 * @return Page<ResourceVo>
	 * @throws Exception 
	 * @author huangt  2014年12月31日
	 */
	public Page<ResourceVo> getCtgResource(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("srcId", map.get("srcId"));
		Page<ResourceVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-ctg-resource", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, ResourceVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课程笔记列表
	 * @param map
	 * @return Page<CourseNoteVo>
	 * @throws Exception 
	 * @author huangt  2014年12月26日
	 */
	public Page<CourseNoteVo> getCourseNoteList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("courseId", map.get("courseId"));
		Page<CourseNoteVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-note", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, CourseNoteVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课程同学列表
	 * @param map
	 * @return Page<CourseClassmateVo>
	 * @throws Exception 
	 * @author huangt  2014年12月29日
	 */
	public Page<CourseClassmateVo> getCourseClassmateList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("courseId", map.get("courseId"));
		Page<CourseClassmateVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-classmate", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, CourseClassmateVo.class);
		return  list;
	}
	
	/**
	 * @description 获得课程评论列表
	 * @param map
	 * @return Page<CommentVo>
	 * @throws Exception 
	 * @author huangt  2014年12月29日
	 */
	public Page<CommentVo> getCourseCommentList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("courseId", map.get("courseId"));
		hashMapParams.put("commontPid", map.get("commontPid"));
		Page<CommentVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-comment", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, CommentVo.class);
		return  list;
	}
	
	/**
	 * @description 根据课件获得资源列表
	 * @param map
	 * @return Page<ResourceVo>
	 * @throws Exception 
	 * @author huangt  2015年1月4日
	 */
	public Page<ResourceVo> getCourseResList(Map<String, Object> map) throws Exception {
		Integer currentPage = (Integer)map.get("pageIndex");
		Integer pageSize  = (Integer)map.get("pageSize");
		Map<String, Object> hashMapParams = new HashMap<String, Object>();
		hashMapParams.put("courseId", map.get("courseId"));
		Page<ResourceVo>  list  =  templateQLService.findByPagedSQLBeanQuery("query-course-resource-list", hashMapParams, Page.getStartOfPage(currentPage, pageSize), pageSize, ResourceVo.class);
		return  list;
	}
	
	@Override
	public Page<StudyCourseVo> getStudyingCourseList(Map<String, Object> params) {
		Integer pageIndex = (Integer) params.get(Constants.PAGE_INDEX_KEY);
		Integer pageSize = (Integer) params.get(Constants.PAGE_SIZE_KEY);
		Page<StudyCourseVo>  pageObject  =  templateQLService.findByPagedSQLBeanQuery("query-studying-courses", params, Page.getStartOfPage(pageIndex, pageSize), pageSize, StudyCourseVo.class);
		return  pageObject;
	}

}

